<template>
	<view>
		 <!-- v-if="loading==true" -->
		 <!-- 首页 -->
		<indexs v-if="state==0" @serbtn="serbtn" :city="city" :more="more" :shoplist="shoplist"></indexs>
		<!-- 我的 -->
		<mys v-if="state==1" :user="user" :channel="channel" :order="order" :tel="tel"></mys>
		<!-- 底部导航 -->
		<navs @jumpnav="jumpnavs"></navs>
		
		 <!-- <loadings  v-if="loading==false"></loadings> -->
	</view>
</template>
<script>
	const app = getApp();
	var util = require('../../utils/util.js');
	import swiperr from '../../components/swipers/swipers.vue';
	var QQMapWX = require('../../utils/qqmap-wx-jssdk.min.js');
	var qqmapsdk = ''
	import loadings from '../../components/loading.vue';
	// 导航
	import navs from '../../components/nav/navs.vue';
	// 首页
	import indexs from './components/index.vue';
	// 我的
	import mys from './components/my.vue';
	export default {
		components: {
			loadings,
			navs,
			indexs,
			mys
		},
		data() {
			return {
				banners:[],
				img_src: app.globalData.img_src,
				img_url: app.globalData.img_urls,
				statusBarHeight: getApp().globalData.statusBarHeight,
				statusBarHeight1: getApp().globalData.statusBarHeight1,
				loading:false,
				page:0,
				//#ifdef H5 || MP-WEIXIN
				key: '2fd9a91cb49b2202c7a478534371e8a6',
				// #endif
				amapPlugin: null,
				latitude:'',
				longitude:'',
				city:'',
				city_id:'',
				// 导航状态
				state:0,
				shoplist:[],
				like:'',
				more:0,
				user:'',
				channel:'',
				order:'',
				channel_id:'',
				channel_url_id:'',
				channel_user_id:'',
				official_id:''
			};
		},
		/**
		 * 生命周期函数--监听页面加载
		 */
		onLoad(options) {
			var that = this;
			// this.channel_id = options.channel_id?options.channel_id:''
			// // options.channel_url_id?options.channel_url_id:
			// this.channel_url_id = ''
			// this.channel_user_id = options.channel_user_id?options.channel_user_id:''
			//console.log(this.channel_id)
			this.official_id = options.official_id?options.official_id:''
			uni.setStorageSync('official_id', this.official_id);
			// uni.setStorageSync('channel_url_id', this.channel_url_id);
			// uni.setStorageSync('channel_user_id', this.channel_user_id);
			if(uni.getStorageSync('official_id')){
				this.official()
			}
		},
		/**
		 * 生命周期函数--监听页面初次渲染完成
		 */
		onReady() {
			
		},
		/**
		 * 生命周期函数--监听页面显示
		 */
		onShow() {
			var that = this;
			//console.log(uni.getStorageSync('city_name'))
			//console.log(uni.getStorageSync('city_id'))
			this.city = uni.getStorageSync('city_name')
			uni.setStorageSync('sjtypes', 0);
			//console.log(uni.getStorageSync('sjtypes'))
			// uni.getStorageSync('city_name')
			// uni.getStorageSync('city_id')
			if(this.state==0){
				that.page = 0
				that.shoplist = []
				this.RestLocation()
				// if(uni.getStorageSync('citytype')==1){
					
				// 	this.shopList()
				// }else{
				// 	this.RestLocation()
				// }
			}else{
				this.Usermy()
			}
			// this.getcity('成都市')
		},
		/**
		 * 生命周期函数--监听页面隐藏
		 */
		onHide() {},
		/**
		 * 生命周期函数--监听页面卸载
		 */
		onUnload() {},
		/**
		 * 页面相关事件处理函数--监听用户下拉动作
		 */
		onPullDownRefresh() {
			this.page = 0;
			uni.hideNavigationBarLoading();
			uni.stopPullDownRefresh();
			if(this.state==0){
				this.shopList()
			}else{
				this.Usermy()
			}
		},
		/**
		 * 页面上拉触底事件的处理函数
		 */
		onReachBottom() {
			this.page++;
			if(this.state==0){
				this.shopList()
			}else{
				this.Usermy()
			}
		},
		/**
		 * 用户点击右上角分享
		 */
		onShareAppMessage(e) {
			
		},
		methods: {
			// 获取appid
			official(){
				app.globalData.api.request('Index/official', {official_id:this.official_id}, 'post', 'form').then((res) => {
					if (res.code == 0) {
						uni.setStorageSync('channel_id', res.data.channel_id);
						uni.setStorageSync('condata', res.data);
						this.shopList()
						this.tel = res.data
					}else{
						app.globalData.show(res.msg)
					}
				});
				app.globalData.api.request('Index/index', {official_id:uni.getStorageSync('official_id')}, 'post', 'form').then((res) => {
					if (res.code == 0) {
						this.tel = res.data.tel
					}else{
						app.globalData.show(res.msg)
					}
				});
			},
			serbtn(e){
				this.page = 0;
				this.like = e
				this.shopList()
			},
			//门店列表
			shopList:function(){
				const that =this;
				app.globalData.api.request('Index/shopList', {
					page:this.page,
					like:this.like,
					lng:uni.getStorageSync('lng')?uni.getStorageSync('lng'):'',
					lat:uni.getStorageSync('lat')?uni.getStorageSync('lat'):'',
					cityid:uni.getStorageSync('city_id'),
					channel_id:uni.getStorageSync('channel_id')
				}, 'post', 'form').then((res) => {
					if (res.code == 0) {
						//console.log(res)
						that.shoplist = this.page>0?that.shoplist.concat(res.data):res.data;
						this.more = res.data.length==0?1:0
					}else{
						app.globalData.show(res.msg)
					}
				});
			},
			Usermy(){
				const that =this;
				app.globalData.api.request('User/my', {}, 'post', 'form').then((res) => {
					if (res.code == 0) {
						//console.log(res)
						that.user = res.data.user;
						that.order = res.data.order
						that.channel = res.data.channel
					}else{
						app.globalData.show(res.msg)
					}
				});
			},
			// 导航事件
			jumpnavs(e){
				this.state = e.detail
				if(this.state==0){
					this.page=0
					this.shopList()
				}else{
					this.Usermy()
				}
			},
			Urbaninquiry() {
			  var that = this;
			  app.globalData.Urbaninquiry(function (res) {
			    //console.log(res)
			    that.lat = res.result.location.lat;
			    that.lng = res.result.location.lng;
			    that.city = res.result.address_component.city
			  })
			},
			//重新定位
			RestLocation(){
				let that = this
				that.shopList()
				// #ifdef H5
				uni.getLocation({
					geocode: true,
					type: 'gcj02',
					success: function(res) {
						that.latitude = res.latitude
						that.longitude = res.longitude
						let lat = res.latitude;
						let lng = res.longitude;
						uni.setStorageSync('lat', lat);
						uni.setStorageSync('lng', lng);
						if(uni.getStorageSync('citytype')==1){
							that.shopList()
						}else{
							// 高德逆地理变码 https://lbs.amap.com/api/webservice/guide/api/georegeo/
							let _key = 'c62bde12b410b83b52f027bb403d9fef'; //高德API key类型：web服务
							uni.request({
								method: 'GET',
								url: 'https://restapi.amap.com/v3/geocode/regeo?parameters',
								data: {
									key: _key,
									location: `${lng},${lat}`,
									output: 'JSON'
								},
								success: async (res) => {
									//console.log(res)
									that.city = res.data.regeocode.addressComponent.city
									that.getcity(that.city)
									uni.hideLoading();
									//用户所在的地理位置信息
								},
								fail: r => {
									//console.log(r);
								}
							});
						}
						
					},
					fail: (err) => {
						//console.log(err)
						that.shopList()
					}
				})
				// #endif
			},
			getcity(city){
				var that = this;
				app.globalData.api.request('Index/getcity', {
					name:city
				}, 'POST', 'form').then((res) => {
					if (res.code == 0) {
						uni.setStorageSync('city_name', res.data.name);
						uni.setStorageSync('city_id', res.data.id);
						that.shopList()
					}else{
						app.globalData.show(res.msg)
					}
				});
			}
		}
	};
</script>
<style>
	.bcktop{
		width: 100%;
		height: 100vh;
		display: table;
		background-color: #f4f4f4;
		position: fixed;
		top: 0px;
		left:0px;
	}
	page{
		background: #f4f4f4;
	}
</style>